<script setup lang="tsx">

import { onMounted, reactive } from 'vue';
import { PageEntity } from '@/entity/page.entity';
import { createClassesApi, deleteClassesApi, getClassesPageApi, updateClassesApi } from '@/service/path/train/classes';
import { useTable } from '@/hooks/common/table';
import { postAction } from '@/service/request/action';
import { useCrud } from '@/hooks/common/crud';
import { getUserListApi } from '@/service/path/admin';
import ClassesSearch from '@/views/train/classes/modules/classes-search.vue';
import ClassesModal from '@/views/train/classes/modules/classes-modal.vue';
import dayjs from 'dayjs';

const state = reactive({
  pageInfo: new PageEntity(),
  opPath: {
    createPath: createClassesApi,
    updatePath: updateClassesApi
  },
  userList: []
});
const {
  columns,
  columnChecks,
  data,
  loading,
  mobilePagination,
  searchParams,
  getData,
  clickSearch,
  clickReset
} = useTable({
  apiFn: () => postAction(getClassesPageApi, state.pageInfo),
  showTotal: true,
  immediate: true,
  apiParams: {
    name: '',
    username: '',
    remark: ''
  },
  pageInfo: state.pageInfo,
  columns: () => [
    {
      title: '序号',
      key: 'index'
    },
    {
      title: '班级名称',
      key: 'name'
    },
    {
      title: '班主任',
      key: 'username'
    },
    {
      title: '年级',
      key: 'schoolYear'
    },
    {
      title: '备注',
      key: 'remark'
    },
    {
      title: '操作',
      key: 'action',
      render: (row: Api.Train.Classes) => <common-operate-column
        row-data={row} onClickEdit={row => clickEdit(row.id, row)}
        onClickDelete={row => clickDelete(row.id)}
      />
    }
  ]
});
const {
  modalFormRef, childRef, clickAdd, clickEdit, clickDelete
} = useCrud({
  uris: {
    delete: deleteClassesApi
  },
  refreshList: getData
});
const getUserList = () => {
  postAction(getUserListApi, {}).then(res => {
    if (!res.error) {
      state.userList = res.data;
    }
  });
};
const otherInitMethod = (row: Api.Train.Classes) => {
  if (row) {
    if (row.schoolYear) {
      childRef.value.state.ruleForm.schoolYear = new Date(row.schoolYear + '-01-01').getTime();
    }
  }
};
const submitTransferData = formData => {
  if (formData.schoolYear) {
    formData.schoolYear = dayjs(formData.schoolYear).format('YYYY');
  }
  return formData;
};
onMounted(() => {
  getUserList();
});
</script>

<template>
  <div class="flex-col-stretch gap-16px lt-sm:overflow-auto">
    <ClassesSearch v-model:model="searchParams" @click-reset="clickReset" @click-search="clickSearch" />
    <n-card title="班级列表" class="flex-1">
      <template #header-extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :loading="loading"
          @click-add="clickAdd"
        />
      </template>
      <n-data-table
        :columns="columns"
        :data="data"
        :loading="loading"
        remote
        :row-key="row => row.id"
        :pagination="mobilePagination"
        class="sm:h-full"
      />
    </n-card>
    <CommonModal ref="modalFormRef" title="班级" :op-path="state.opPath" @refresh-list="getData"
                :other-init-method="otherInitMethod" :submit-transfer-data="submitTransferData">
      <ClassesModal ref="childRef"  :user-list="state.userList"/>
    </CommonModal>
  </div>
</template>

<style scoped>

</style>
